package cn.itsource.pay.controller;

import cn.itsource.order.domain.ProductOrder;
import cn.itsource.order.service.IProductOrderService;
import cn.itsource.pay.contants.BusinessType;
import cn.itsource.pay.domain.PayBill;
import cn.itsource.pay.service.IAliPayInfoService;
import cn.itsource.pay.service.IPayBillService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

@Controller
@RequestMapping("/alipay")
public class AliPayController {

    @Autowired
    private IAliPayInfoService aliPayInfoService;

    @Autowired
    private IPayBillService payBillService;

    @Autowired
    private IProductOrderService productOrderService;

    //异步通知
    @RequestMapping("/notify")
    public void aliPayreturn(HttpServletRequest request) throws Exception {
        //处理请求参数
        Map<String,String> params = new HashMap<String,String>();
        Map<String,String[]> requestParams = request.getParameterMap();
        for (Iterator<String> iter = requestParams.keySet().iterator(); iter.hasNext();) {
            String name = (String) iter.next();
            String[] values = (String[]) requestParams.get(name);
            String valueStr = "";
            for (int i = 0; i < values.length; i++) {
                valueStr = (i == values.length - 1) ? valueStr + values[i]
                        : valueStr + values[i] + ",";
            }
            //乱码解决，这段代码在出现乱码时使用
            valueStr = new String(valueStr.getBytes("ISO-8859-1"), "utf-8");
            params.put(name, valueStr);
        }

        //获取支付的订单号
        String out_trade_no = new String(request.getParameter("out_trade_no").getBytes("ISO-8859-1"),"UTF-8");
        System.out.println("商户订单号："+out_trade_no);
        PayBill payBill = payBillService.queryByOrderSn(out_trade_no);
        //获取支付宝订单号
        String trade_no = new String(request.getParameter("trade_no").getBytes("ISO-8859-1"),"UTF-8");
        //订单的交易状态
        String trade_status = new String(request.getParameter("trade_status").getBytes("ISO-8859-1"),"UTF-8");
        if (trade_status.equals("TRADE_SUCCESS")){
            //改支付的状态
            payBill.setState(1);
            payBill.setUnionPaySn(trade_no);
            payBill.setUpdateTime(new Date());
            payBillService.update(payBill);
            //改订单的状态
            if (payBill.getBusinessType().equals(BusinessType.BUSINESS_PRODUCT)){
                ProductOrder productOrder = productOrderService.queryById(payBill.getBusinessKey());
                productOrder.setState(1);
                productOrder.setPaySn(trade_no);
                productOrderService.update(productOrder);
            }else if(payBill.getBusinessType().equals(BusinessType.BUSINESS_PET)){
                //使用pet相关的service 查询pet相关的表
            }
        }
    }


    //同步通知
    @RequestMapping("/return")
    public String aliPayNotify(){

        return "redirect:http//localhost/success.html";
    }
}
